package com.bvgol.examples.springboot.args.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DuplicationInterceptor implements HandlerInterceptor {
    long start = 0L;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        start = System.currentTimeMillis();
        // 获取请求的 URI
        String uri = request.getRequestURI();

        // 判断请求的 URI 是否已经是目标 URI
        if (uri.contains("/xxxx")) {
            // 如果请求的 URI 不包含目标路径，则进行修改
            String modifiedUri = uri.replace("/xxxx", "");
// 获取请求头中的特定信息，例如 Authorization
            String authorizationHeader = request.getHeader("Authorization");
            // 发起转发请求
//            request.getRequestDispatcher(uri).forward(request, response);

            // 返回 false，告知 Spring MVC 不再继续处理当前请求
            return true;
        }

        // 返回 true，继续处理当前请求
        return true;

    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        System.out.println("Interceptor cost=" + (System.currentTimeMillis() - start));
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    }
}
